package com.hooda.container;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 一个站点下的配置数据
 * @author zhangzp
 * @version 1.0, 2021/10/15
 */
public class Host {

    private String appBase = "";

    private Context empty = new Context(null);

    private Map<String, Context> contexts = new HashMap<>();

    public Host(String appBase) {
        this.appBase = appBase;
        init();
    }

    private void init() {
        if (null == appBase) {
            return;
        }

        File root = new File(appBase);
        if (root.isDirectory()) {
            File[] files = root.listFiles();
            for (File file : files) {
                if (file.isDirectory()) {
                    contexts.put(file.getName(), new Context(file.getPath()));
                }
            }
        }
    }

    public Context getContext(String contextName) {
        if (contexts.containsKey(contextName)) {
            return contexts.get(contextName);
        }
        return empty;
    }
}
